import 'package:flutter/material.dart';
import 'package:flutter_picker/picker.dart';

import '../plugin_demos/example_flutter_picker.dart';

class TempPage extends StatefulWidget {
  const TempPage({super.key});

  @override
  State<TempPage> createState() => _TempPageState();
}

class _TempPageState extends State<TempPage> {

  String? pickerResult;
  final _textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Temp Page")),
      body: Container(
        width: double.infinity,
        color: Colors.blue.shade50,
        child: Column(
          children: [
            Container(
              padding: const EdgeInsets.all(20),
              margin: const EdgeInsets.only(bottom: 20),
              color: Colors.white,
              child: Row(
                children: [
                  const Text("picker"),
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        List<MyEntity>? dictItems = [];
                        dictItems.add(MyEntity(itemText: "论休假", itemValue: "lunxiu"));
                        dictItems.add(MyEntity(itemText: "同步假期", itemValue: "tongbu"));
            
                        List<PickerItem<String>> items = [];
                        for (var a in dictItems) {
                          items.add(PickerItem(text: Text(a.itemText ?? ''), value: a.itemValue));
                        }
            
                        Picker(
                            height: 100,
                            // adapter: PickerDataAdapter<String>(data: items, isArray: false),
                            adapter: PickerDataAdapter<String>(
                                pickerData: dictItems.map((item) => item.itemText).toList()),
                            hideHeader: true,
                            itemExtent: 42,
                            title: const Text("请假类型"),
                            confirmText: '确定',
                            cancelText: '取消',
                            // selectedTextStyle: const TextStyle(color: Colors.black, fontSize: 20),
                            onConfirm: (Picker picker, List value) {
                              setState(() {
                                pickerResult = picker.getSelectedValues()[0];
                              });
                            }).showDialog(context);
                      },
                      child: SizedBox(
                        width: double.infinity,
                        child: Align(
                          alignment: Alignment.centerRight,
                          child: Text(pickerResult ?? '')
                        ),
                      ),
                    )
                  )
                ]
              ),
            ),
            Container(
              padding: const EdgeInsets.all(20),
              color: Colors.white,
              child: Row(
                children: [
                  const Text("editor"),
                  Expanded(
                    child: TextField(
                      controller: _textController,
                      onTapOutside: (event) {
                        FocusScope.of(context).requestFocus(FocusNode());
                      }
                    )
                  )
                ]
              ),
            )
          ]
        ),
      )
    );
  }
}

class TestFlowDelegate extends FlowDelegate {
  EdgeInsets margin;

  TestFlowDelegate({this.margin = EdgeInsets.zero});

  double width = 0;
  double height = 0;

  @override
  void paintChildren(FlowPaintingContext context) {
    var x = margin.left;
    var y = margin.top;
    //计算每一个子widget的位置
    for (int i = 0; i < context.childCount; i++) {
      var w = context.getChildSize(i)!.width + x + margin.right;
      if (w < context.size.width) {
        context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
        x = w + margin.left;
      } else {
        x = margin.left;
        y += context.getChildSize(i)!.height + margin.top + margin.bottom;
        //绘制子widget(有优化)
        context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
        x += context.getChildSize(i)!.width + margin.left + margin.right;
      }
    }
  }

  @override
  Size getSize(BoxConstraints constraints) {
    // 指定Flow的大小，简单起见我们让宽度尽可能大，但高度指定为200，
    // 实际开发中我们需要根据子元素所占用的具体宽高来设置Flow大小
    return const Size(double.infinity, 200.0);
  }

  @override
  bool shouldRepaint(FlowDelegate oldDelegate) {
    return oldDelegate != this;
  }
}
